package com.reactor;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptorHandler implements Runnable{
    private ServerSocketChannel serverSocketChannel ;
    private Selector selector;
    public AcceptorHandler(Selector selector, ServerSocketChannel serverSocketChannel) throws IOException {
        this.selector = selector;
        this.serverSocketChannel = serverSocketChannel;
    }

    @Override
    public void run() {
        try {
            SocketChannel socketChannel = serverSocketChannel.accept();
            if(socketChannel!=null){
                SelectionKey key = null;
                socketChannel.configureBlocking(false);
                key = socketChannel.register(selector, 0);
                key.attach(new MyHandler(socketChannel, selector));
                key.interestOps(SelectionKey.OP_READ);
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
